package system1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type WorkflowLogApi struct {
}

var WorkflowLogService = service.ServiceGroupApp.System1ServiceGroup.WorkflowLogService

// CreateWorkflowLog 创建审批历史
// @Tags WorkflowLog
// @Summary 创建审批历史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowLog true "创建审批历史"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /WorkflowLog/createWorkflowLog [post]
func (WorkflowLogApi *WorkflowLogApi) CreateWorkflowLog(c *gin.Context) {
	var WorkflowLog system1.WorkflowLog
	err := c.ShouldBindJSON(&WorkflowLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowLog.CreatedBy = utils.GetUserID(c)

	if err := WorkflowLogService.CreateWorkflowLog(&WorkflowLog); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteWorkflowLog 删除审批历史
// @Tags WorkflowLog
// @Summary 删除审批历史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowLog true "删除审批历史"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /WorkflowLog/deleteWorkflowLog [delete]
func (WorkflowLogApi *WorkflowLogApi) DeleteWorkflowLog(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := WorkflowLogService.DeleteWorkflowLog(ID, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteWorkflowLogByIds 批量删除审批历史
// @Tags WorkflowLog
// @Summary 批量删除审批历史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /WorkflowLog/deleteWorkflowLogByIds [delete]
func (WorkflowLogApi *WorkflowLogApi) DeleteWorkflowLogByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	if err := WorkflowLogService.DeleteWorkflowLogByIds(IDs, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateWorkflowLog 更新审批历史
// @Tags WorkflowLog
// @Summary 更新审批历史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowLog true "更新审批历史"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /WorkflowLog/updateWorkflowLog [put]
func (WorkflowLogApi *WorkflowLogApi) UpdateWorkflowLog(c *gin.Context) {
	var WorkflowLog system1.WorkflowLog
	err := c.ShouldBindJSON(&WorkflowLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowLog.UpdatedBy = utils.GetUserID(c)

	if err := WorkflowLogService.UpdateWorkflowLog(WorkflowLog); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindWorkflowLog 用id查询审批历史
// @Tags WorkflowLog
// @Summary 用id查询审批历史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.WorkflowLog true "用id查询审批历史"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /WorkflowLog/findWorkflowLog [get]
func (WorkflowLogApi *WorkflowLogApi) FindWorkflowLog(c *gin.Context) {
	ID := c.Query("ID")
	if reWorkflowLog, err := WorkflowLogService.GetWorkflowLog(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reWorkflowLog": reWorkflowLog}, c)
	}
}

// GetWorkflowLogList 分页获取审批历史列表
// @Tags WorkflowLog
// @Summary 分页获取审批历史列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1Req.WorkflowLogSearch true "分页获取审批历史列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /WorkflowLog/getWorkflowLogList [get]
func (WorkflowLogApi *WorkflowLogApi) GetWorkflowLogList(c *gin.Context) {
	var pageInfo system1Req.WorkflowLogSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := WorkflowLogService.GetWorkflowLogInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
